package com.sunny.example.dynamicschedule;

import java.util.HashMap;
import java.util.Scanner;

/**
 * 最长连续子串
 * 动态规划
 */
public class DynamicSchedule {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str1 = "";
            int k = 0;
            int maxLen = 0;
            HashMap<Integer, Integer> hashMap = new HashMap<>();//连加和，下标

            String[] subInts = null;
            for (int i = 2; i > 0; i--) {
                if (i == 2) {
                    str1 = scanner.nextLine();
                    subInts = str1.split(",");
                } else {
                    k = Integer.parseInt(scanner.nextLine());
                }
            }
            if (subInts == null || subInts.length == 0) {
                continue;
            }

            int sum = 0;
            for (int i = 0; i < subInts.length; i++) {
                sum += Integer.parseInt(subInts[i]);

                if (sum == k) {
                    maxLen = i + 1;
                    continue;
                }
                if (!hashMap.containsKey(sum)) {
                    hashMap.put(sum, i);
                }

                if (hashMap.containsKey((sum - k))) {//更改状态
                    maxLen = Math.max(maxLen, i - hashMap.get(sum - k));
                }
            }
            int result = maxLen == 0 ? -1 : maxLen;
            System.out.println(result);
        }
    }
}
